package com.huawei.hms.aaid;

import android.app.Activity;
import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import com.huawei.hms.aaid.b.e;
import com.huawei.hms.aaid.c.a;
import com.huawei.hms.aaid.c.b;
import com.huawei.hms.aaid.c.c;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.hms.aaid.constant.AaidIdConstant.PUSH_CLIENT_SELF_INFO;
import com.huawei.hms.aaid.entity.AAIDResult;
import com.huawei.hms.aaid.entity.DeleteTokenReq;
import com.huawei.hms.aaid.entity.TokenResult;
import com.huawei.hms.api.Api;
import com.huawei.hms.api.Api.ApiOptions.NoOptions;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.common.HuaweiApi;
import com.huawei.hms.common.internal.Preconditions;
import com.huawei.hms.core.aidl.IMessageEntity;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.JsonUtil;
import com.huawei.hms.utils.Util;
import o.fjo;
import o.fjw;
import o.fjx;

public class HmsInstanceId {
    public static final String TAG = HmsInstanceId.class.getSimpleName();
    private static final byte[] a = new byte[0];
    private static final byte[] b = new byte[0];
    private Context c = null;
    private c d = null;
    private HuaweiApi<NoOptions> e;

    private HmsInstanceId(Context context) {
        this.c = context.getApplicationContext();
        this.d = new c(context, "aaid");
        Api api = new Api(HuaweiApiAvailability.HMS_API_NAME_PUSH);
        if (context instanceof Activity) {
            this.e = new HuaweiApi((Activity) context, api, null, new e());
        } else {
            this.e = new HuaweiApi(context, api, null, new e());
        }
        this.e.setKitSdkVersion(30003301);
    }

    public static HmsInstanceId getInstance(Context context) {
        Preconditions.checkNotNull(context);
        return new HmsInstanceId(context);
    }

    public String getId() {
        return a.b(this.c);
    }

    public fjo<AAIDResult> getAAID() {
        try {
            return fjw.ˋ(new com.huawei.hms.aaid.b.a(this.c.getApplicationContext()));
        } catch (Exception e) {
            fjx o_fjx = new fjx();
            o_fjx.ॱ(com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR));
            return o_fjx.ॱ();
        }
    }

    public long getCreationTime() {
        try {
            if (!this.d.c(AaidIdConstant.CREATE_TIME)) {
                getAAID();
            }
            return this.d.b(AaidIdConstant.CREATE_TIME);
        } catch (Exception e) {
            return 0;
        }
    }

    public void deleteAAID() throws ApiException {
        try {
            if (this.d.c("aaid")) {
                this.d.d("aaid");
                this.d.d(AaidIdConstant.CREATE_TIME);
            }
        } catch (RuntimeException e) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
        } catch (Exception e2) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
        }
    }

    @Deprecated
    public String getToken() {
        String str = null;
        try {
            str = getToken(null, null);
        } catch (Exception e) {
        }
        return str;
    }

    public String getToken(String str, String str2) throws ApiException {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_MAIN_THREAD);
        }
        String a = b.a(this.c, AaidIdConstant.getToken);
        try {
            IMessageEntity a2 = a.a(str, str2, this.c);
            a2.setAaid(getId());
            HMSLog.d(TAG, "getToken req :" + a2.toString());
            return ((TokenResult) fjw.ॱ(this.e.doWrite(new com.huawei.hms.aaid.b.c(AaidIdConstant.getToken, JsonUtil.createJsonString(a2), this.c, a)))).getToken();
        } catch (Exception e) {
            if (e.getCause() instanceof ApiException) {
                ApiException apiException = (ApiException) e.getCause();
                b.a(this.c, AaidIdConstant.getToken, a, apiException.getStatusCode());
                throw apiException;
            }
            b.a(this.c, AaidIdConstant.getToken, a, com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
        }
    }

    public void deleteToken(String str, String str2) throws ApiException {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_MAIN_THREAD);
        }
        String a = b.a(this.c, AaidIdConstant.deleteToken);
        try {
            IMessageEntity deleteTokenReq = new DeleteTokenReq();
            deleteTokenReq.setAppId(str);
            deleteTokenReq.setScope(str2);
            deleteTokenReq.setPkgName(this.c.getPackageName());
            if (TextUtils.isEmpty(str)) {
                deleteTokenReq.setAppId(Util.getAppId(this.c));
            }
            if (TextUtils.isEmpty(str2)) {
                deleteTokenReq.setScope(AaidIdConstant.DEFAULT_SCOPE_TYPE);
            }
            Object a2 = com.huawei.hms.aaid.a.c.a(this.c, PUSH_CLIENT_SELF_INFO.FILE_NAME);
            if (!TextUtils.isEmpty(a2)) {
                deleteTokenReq.setToken(a2);
            }
            fjw.ॱ(this.e.doWrite(new com.huawei.hms.aaid.b.b(AaidIdConstant.deleteToken, JsonUtil.createJsonString(deleteTokenReq), a)));
            com.huawei.hms.aaid.a.c.b(this.c, PUSH_CLIENT_SELF_INFO.FILE_NAME);
        } catch (Exception e) {
            if (e.getCause() instanceof ApiException) {
                ApiException apiException = (ApiException) e.getCause();
                b.a(this.c, AaidIdConstant.deleteToken, a, apiException.getStatusCode());
                throw apiException;
            }
            b.a(this.c, AaidIdConstant.deleteToken, a, com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
        }
    }
}
